package com.test.security.springsecuritydemo12.service;

import com.test.security.springsecuritydemo12.domain.User;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @since 2023/1/5 上午11:14
 */
@Component
public class AuthenticationServerProxy {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${auth.server.base.url}")
    private String baseUrl;

    /**
     * Send auth.
     *
     * @param username the username
     * @param passwd   the passwd
     */
    public void sendAuth(String username, String passwd) {
        String url = baseUrl + "/user/auth";
        var body = new User();
        body.setUsername(username);
        body.setPassword(passwd);
        var request = new HttpEntity<>(body);
        restTemplate.postForEntity(url, request, Void.class);
    }

    /**
     * Send otp boolean.
     *
     * @param username the username
     * @param code     the code
     * @return the boolean
     */
    public boolean sendOtp(String username, String code) {
        String url = baseUrl + "/otp/check";
        var body = new User();
        body.setUsername(username);
        body.setCode(code);
        var request = new HttpEntity<>(body);
        final ResponseEntity<Void> response = restTemplate.postForEntity(url, request, Void.class);
        return Objects.equals(response.getStatusCode(), HttpStatus.OK);
    }

}
